import unittest
import requests
import time
class User(unittest.TestCase):
    base_url = "http://182.151.48.185:8098/api/private/v1/"

    @classmethod
    def setUpClass(cls):
        token = cls.login(cls)
        cls.header = {
            "Authorization": token
        }
        # cls.header["Authorization"] = token

    def login(self):
        url = self.base_url + "login"
        mydata = {
            "username": "admin",
            "password": "123456"
        }
        url = self.base_url + "login"

        res = requests.post(url, data=mydata)
        assert res.status_code == 200
        assert "登录成功" in res.text
        # print(res.text)
        # print(res.json().get("data").get("token"))
        token = res.json().get("data").get("token")
        return token

    def test_addUser(self):
        url = self.base_url + "users"
        now = time.localtime()
        formattime = time.strftime("%Y%m%d%H%M%S", now)
        userinfo = {
            "username": "123" + formattime,
            "password": "123456",
            "email": "123456@qq.com",
            "mobile": "12345678910"
        }

        res = requests.post(url, headers=self.header, json=userinfo)
        self.assertEqual(200, res.status_code)
        self.assertIn("创建成功", res.text)
        print(res.text)

    def test_searchUser(self):
        url = self.base_url + "users/2301"

        # userinfo = {
        #     "id": 2301
        # }
        res = requests.get(url, headers=self.header)
        self.assertEqual(200, res.status_code)
        self.assertIn("获取成功", res.text)
        print(res.text)

if __name__ == "__main__":
    unittest.main()